package ma.co.cdm.esign.workers;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.mail.internet.MimeMessage;

import ma.co.cdm.esign.vo.MySisSolrDoc;
import ma.co.cdm.esign.vo.SolrBean;

import org.apache.commons.io.IOUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailProcessing {

	private String hostName;
	private String port;
	private String user;
	private String password;
	private String encoding;
	private String mailFrom;
	private String mailTemplateRejet;
	private String mailToTest;
	
	private final static String OBJET_DELIMITER="#OBJECT#";
	private final static String BODY_DELIMITER="#BODY#";
	private final static String CLIENT_DELIMITER="#CLIENT#";
	private final static String CAUSE_REJET_DELIMITER="#CAUSE#";
	
	 public MailProcessing() {
		super();
		 InputStream stream = getClass().getClassLoader().getResourceAsStream("mail/mailTemplateRejet");
		 StringWriter writer = new StringWriter();
		 try {
			IOUtils.copy(stream, writer);
		} catch (IOException e) {		
			e.printStackTrace();
		}
		 mailTemplateRejet = writer.toString();
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getMailTemplateRejet() {
		return mailTemplateRejet;
	}

	public void setMailTemplateRejet(String mailTemplateRejet) {
		this.mailTemplateRejet = mailTemplateRejet;
	}

	public String getMailFrom() {
		return mailFrom;
	}

	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}
    
	public String getMailToTest() {
		return mailToTest;
	}

	public void setMailToTest(String mailToTest) {
		this.mailToTest = mailToTest;
	}

	public boolean sendMail(SolrBean bean) {
		 try {
			 
			   JavaMailSenderImpl sender = new JavaMailSenderImpl();
			   sender.setHost(getHostName());
			   sender.setPort(Integer.parseInt(getPort()));
			   sender.setUsername(getUser());
			   sender.setPassword(getPassword());
			   sender.setDefaultEncoding(getEncoding());
			   MimeMessage message = sender.createMimeMessage();
			   MimeMessageHelper helper = new MimeMessageHelper(message,true);			   
			   helper.setFrom(getMailFrom());
			   helper.addTo(getMailTo());
			   int objIndex = getMailTemplateRejet().toUpperCase().indexOf(OBJET_DELIMITER);
			   int bodyndex = getMailTemplateRejet().toUpperCase().indexOf(BODY_DELIMITER);
			   String objString = getMailTemplateRejet().substring(objIndex+OBJET_DELIMITER.length(), bodyndex);
			   objString = objString.replaceAll("\r", "");
			   objString = objString.replaceAll("\n", "");
			   helper.setSubject(objString);	
			   String bodyMail = getMailTemplateRejet().substring(bodyndex+BODY_DELIMITER.length());				
			   bodyMail = bodyMail.replaceAll(CLIENT_DELIMITER, ((MySisSolrDoc)bean).getApplicantName());
			   bodyMail = bodyMail.replaceAll(CAUSE_REJET_DELIMITER, ((MySisSolrDoc)bean).getValidationMessage());
			   helper.setText(bodyMail);
			   sender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		 return true;
	}

	
	public String getMailTo()
	{
		//TODO : get trade center mail from the BCT,
		//               the trade center should be the one where the applicant is registred
		
		return getMailToTest();
	}
	
//	 public static void main(String[] args) {
//		 MailProcessing mailSender = new MailProcessing();
//		   String[] tokens = mailSender.getMailTemplateRejet().split("#OBJECT#");
//		   int objIndex = mailSender.getMailTemplateRejet().toUpperCase().indexOf("#OBJECT#");
//		   int bodyndex = mailSender.getMailTemplateRejet().toUpperCase().indexOf("#BODY#");
//		   
////		   System.out.println(objIndex);
////		   System.out.println(bodyndex);
//		   String objString = mailSender.getMailTemplateRejet().substring(objIndex+"#OBJECT#".length(), bodyndex);
//		   objString = objString.replaceAll("\r", "");
//		   objString = objString.replaceAll("\n", "");
//		   System.out.println(objString);
//		   
//		   String bodyString = mailSender.getMailTemplateRejet().substring(bodyndex+"#BODY#".length());
//		   System.out.println(bodyString);
////		   for (int i = 0; i < tokens.length; i++) {
////			String token = tokens[i];
////			 System.out.println(token);
////			   System.out.println(i+"    ---------------------------------");
////		}
//		  
//	       
//	        
//	       
//	}

}
